<?php namespace Home\Controller; 
//商品表
use \Common\Model\Goods;

//品牌表
use \Common\Model\Brand;

//类型属性表
use \Common\Model\TypeAttr;

//分类表
use \Common\Model\Cate;

//商品属性表
use \Common\Model\GoodsAttr;
//列表控制器
class ListPageController extends CommonController{
    private $Model;
//  自定义构造函数
    public function __auto(){
        //引Model文件					//商品详细表
        $this->Model = new \Home\Model\ListPage;
    } 
    
//  首页
    public function index(){	
//		1.获取顶级分类
		$cateModel = new Cate();
		$cateData = $cateModel->where("pid=0")->get();
//		p($cateData);
		
		
//		2.通过当前的cid获得所有的子集id
		$cid = Q("get.cid",0,'intval');
		$cids = $cateModel->getSon($cateModel->get(),$cid);
		$cids[] = $cid;
//		p($cids);
//		3.根据cid获得商品的gid
		
			$goodsModel = new Goods();

			$aids = $goodsModel->where("category_cid IN(". implode(',', $cids). ")")->lists('aid');
//			4.通过aid查询商品属性表
//			p($aids);
			if($aids){
				$goodsAttrModel = new GoodsAttr();
				$goodsAttr = $goodsAttrModel->where("goods_aid IN(" .implode(',', $aids). ")")->groupBy('gtvalue')->get();
				//把相同taid的属性放在一起
				$sameTemp = [];
				foreach ($goodsAttr as $v) {
					$sameTemp[$v['type_attr_taid']][]=$v;
				}
//				p($sameTemp);
			//组合名字和值
				$typeAttrModel = new TypeAttr();
				$finalAttr = [];
				foreach ($sameTemp as $taid => $v) {
					$finalAttr[]=[
						'name'=>$typeAttrModel->where("taid={$v[0]['type_attr_taid']}")->pluck('taname'),
						'value'=>$v
					];
				}
				
//				p($finalAttr);
				View::with('finalAttr',$finalAttr);
				//--筛选效果--
//				1.地址栏变为0.0_
					//获得数组个数
					$num = count($finalAttr);
//					p($num);
					$param = isset($_GET['param']) ?explode('_', $_GET['param']) : array_fill(0, $num, 0);
//					p($param);
					View::with('param',$param);
//				2.筛选操作,最终获得商品		filter方法(P94) //商品表的id
					$finalAids = $this->filter($param,$aids);
//					p($finalAids);
//				3.显示筛选商品


					if($finalAids){
						$finalData = $goodsModel
								->join('brand','brand_bid','=','bid')
								->where("aid IN(". implode(',', $finalAids). ")")->get();
//						p($finalData);
//					共多少件商品
						$number = count($finalData);
//						p($number);
						if($number){
							View::with('number',$number);
						}else{
							View::with('number',0);
						}
					
						View::with('finalData',$finalData);
					}else{
						View::with('finalData',[]);
					}
					
			}else{
				View::with('finalAttr',[]);
			}
			
		//随机商品
//		$shopOrder = Db::select("select * from shop_detail as s join goods as g on s.goods_aid=g.aid join category sd c on g.category_cid=c.cid order By rand() limit 2");
		$shopOrder = $goodsModel
					->join('shop_detail as sd','sd.goods_aid','=','goods.aid')
					->join('category as c','goods.category_cid','=','cid')
					->where("cid IN(".implode(',', $cids) .")")
					->orderBy('rand()')
					->limit(4)
					->get();
//		p($shopOrder);
		View::with('shopOrder',$shopOrder);
        View::make();
    }								
//  点击获得商品					//商品表的id
	public function filter($param,$cidAids){
//	    p($param);
//		p($cidAids);
		$goodsAttrModel = new GoodsAttr();
		$aidAttr = [];
		foreach ($param as $gtid) {
			//如果点击的不是'不限'的时候
//			根据gtid 得到商品的aid
			if($gtid){
				//查询到商品属性的名称
				$gtvalue = $goodsAttrModel->where("gtid={$gtid}")->pluck('gtvalue');
//				p($gtvalue);
				//通过名称查询到goods_aid
				$aidAttr[] = $goodsAttrModel->where(['gtvalue'=>$gtvalue])->lists('goods_aid');
//				p($aidAttr);
			}
		}
		//不是全部"不限"的时候
			if($aidAttr){
//				p($aidAttr);
				$aid = $aidAttr[0];
				foreach ($aidAttr as $v) {
	//				交集
					$aid = array_intersect($v, $aid);
				}
				//因为$aid有可能是其他分类的aid,确保是当前的分类再一次进行交集
				$finalAids = array_intersect($aid, $cidAids);
//				p($finalAids);
			}else{//如果全部是"不限"的时候
				$finalAids = $cidAids;
			}
			return $finalAids;
	}
	
	
	//搜索
	public function search(){
		$key = Q("get.k",'');
	    $goodsModel = new Goods();
		$aids = $goodsModel->where("shopname like '%{$key}%'")->lists('aid');
		$cids = $goodsModel->where("shopname like '%{$key}%'")->lists('category_cid');
				
		if($cids){
			$cid = implode(',', array_unique($cids));
			$cids = $this->get_parentid($cid);
		}
		
//		p($cids);
		View::with('cids',$cids);

//			4.通过aid查询商品属性表
			if($aids){
				$goodsAttrModel = new GoodsAttr();
				$goodsAttr = $goodsAttrModel->where("goods_aid IN(" .implode(',', $aids). ")")->groupBy('gtvalue')->get();
				//把相同taid的属性放在一起
				$sameTemp = [];
				foreach ($goodsAttr as $v) {
					$sameTemp[$v['type_attr_taid']][]=$v;
				}
//				p($sameTemp);
			//组合名字和值
				$typeAttrModel = new TypeAttr();
				$finalAttr = [];
				foreach ($sameTemp as $taid => $v) {
					$finalAttr[]=[
						'name'=>$typeAttrModel->where("taid={$v[0]['type_attr_taid']}")->pluck('taname'),
						'value'=>$v
					];
				}
				
//				p($finalAttr);
				View::with('finalAttr',$finalAttr);
				//--筛选效果--
//				1.地址栏变为0.0_
					//获得数组个数
					$num = count($finalAttr);
//					p($num);
					$param = isset($_GET['param']) ?explode('_', $_GET['param']) : array_fill(0, $num, 0);
//					p($param);
					View::with('param',$param);
//				2.筛选操作,最终获得商品		filter方法(P94) //商品表的id
					$finalAids = $this->filter($param,$aids);
//					p($finalAids);
//				3.显示筛选商品
					if($finalAids){
						$finalData = $goodsModel
								->join('brand','brand_bid','=','bid')
								->where("aid IN(". implode(',', $finalAids). ")")->get();
//						p($finalData);
//					共多少件商品
						$number = count($finalData);
//						p($number);
						if($number){
							View::with('number',$number);
						}else{
							View::with('number',0);
						}
					
						View::with('finalData',$finalData);
					}else{
						View::with('finalData',[]);
					}
					
			}else{
				View::with('finalAttr',[]);
			}
			
		//随机商品
		if(isset($cid)){
			$shopOrder = $goodsModel
					->join('shop_detail as sd','sd.goods_aid','=','goods.aid')
					->join('category as c','goods.category_cid','=','cid')
					->where("cid = {$cid}")
					->orderBy('rand()')
					->limit(4)
					->get();
			View::with('shopOrder',$shopOrder);	
		}else{
			$cid = 1;
			$shopOrder = $goodsModel
					->join('shop_detail as sd','sd.goods_aid','=','goods.aid')
					->join('category as c','goods.category_cid','=','cid')
					->where("cid = {$cid}")
					->orderBy('rand()')
					->limit(4)
					->get();
			View::with('shopOrder',$shopOrder);	
			
		}
		
			
		View::make();
	}

//		获得父级id
		public function get_parentid($cid){
			$cateModel = new Cate();
		    $cid = $cateModel->find($cid);
		    if($cid['pid'] != 0){ return $this->get_parentid($cid['pid']); }
		    return $cid['cid'];
		}

}